package com.mayikt.base;

import com.mayikt.constants.Constants;
import nl.bitwalker.useragentutils.Browser;
import nl.bitwalker.useragentutils.UserAgent;
import nl.bitwalker.useragentutils.Version;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;

public class BaseWebController {

    protected static final String ERROR_500_FTL = "500.ftl";
    public Boolean success(BaseResponse<?> baseResponse){
        if (null == baseResponse) {
            return false;
        }
        if (!Constants.HTTP_RES_CODE_200.equals(baseResponse.getCode())) {
            return false;
        }
        return true;
    }

    public void setErrorMsg(Model model, String errorMsg){
        model.addAttribute("error",errorMsg);
    }

    /**
     * 获取浏览器信息
     * @param request
     * @return
     */
    public String webBrowserInfo(HttpServletRequest request){

        Browser browser = UserAgent.parseUserAgentString(request.getHeader("User-Agent")).getBrowser();
        Version version = browser.getVersion(request.getHeader("User-Agent"));
        String info = browser.getName() + "/" + version.getVersion();
        return info;
    }
}
